<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表
class Bbs_users_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'users';
        $this->users_info = 'users_info';
        $this->bbs_account = 'bbs_account';
        $this->bbs_count = 'bbs_count';
        $this->bbs_report = 'bbs_report';
        $this->bbs_user = 'bbs_user';
        $this->bbs_user_group = 'bbs_user_group';
        $this->bbs_letter = 'bbs_letter';
    }

    //bbs用户统计
    function get_bbs_count($where,$like){
        $this->db->select($this->bbs_account.'.*');
        $this->db->from($this->bbs_account);
        $this->db->join($this->table,$this->bbs_account.'.user_id='.$this->table.'.user_id','left');
        $this->db->join($this->users_info,$this->bbs_account.'.user_id='.$this->users_info.'.user_id','left');
        $this->db->where($where)->like($this->table.'.mobile',$like);
        return $this->db->get()->num_rows();
    }
    function get_bbs_list($where,$page,$limit,$like=""){
        $this->db->select($this->bbs_account.'.*,'.$this->table.".username,".$this->table.".mobile,".$this->users_info.".nickname,".$this->bbs_count.".*,".$this->bbs_user.".group_id");
        $this->db->from($this->bbs_account);
        $this->db->join($this->table,$this->bbs_account.'.user_id='.$this->table.'.user_id','left');
        $this->db->join($this->users_info,$this->bbs_account.'.user_id='.$this->users_info.'.user_id','left');
        $this->db->join($this->bbs_count,$this->bbs_account.'.user_id='.$this->bbs_count.'.user_id','left');
        $this->db->join($this->bbs_user,$this->bbs_account.'.user_id='.$this->bbs_user.'.user_id','left');
        if($like){
            $this->db->like($this->table.'.mobile',$like);
        }
        $this->db->where($where);
        $this->db->order_by('bbs_account.id desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    function get_user_by_uid($uid=0){
        return $this->db->select('*')->from($this->table)->where('user_id',$uid)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_username($username){
        return $this->db->select('*')->from($this->table)->where('username',$username)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_mobile($mobile){
        return $this->db->select('*')->from($this->table)->where('mobile',$mobile)->order_by('reg_time desc')->get()->row_array();
    }
    
    function get_user_by_email($email){
        return $this->db->select('*')->from($this->table)->where('email',$email)->order_by('reg_time desc')->get()->row_array();
    }
    //bbs用户举报
    function get_report_count(){
        return $this->db->get($this->bbs_report)->num_rows();
    }
    function get_report_list($page,$limit){
        $this->db->select($this->bbs_report.'.*,'.$this->table.'.username');
        $this->db->from($this->bbs_report);
        $this->db->join($this->table,$this->bbs_report.'.user_id='.$this->table.'.user_id','left');
        $this->db->order_by('create_time desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    function setstar($star,$id){
        return $this->db->where_in('user_id',$id)->update($this->bbs_account,array('star'=>$star));
    }
    //更新数据
    function update($data,$user_id){
        return $this->db->where('user_id',$user_id)->update($this->bbs_user, $data);
    }
    function Get($user_id){
        return $this->db->select('*')
          ->from($this->bbs_user)
          ->where('user_id' , $user_id)
          ->get()
          ->row_array();
    }
    function get_group_List(){
        $this->db->select('*');
        $this->db->from($this->bbs_user_group);
        $this->db->order_by('id asc');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_user_info($user_id){
        return $this->db->select('*')
          ->from($this->users_info)
          ->where('user_id' , $user_id)
          ->get()
          ->row_array();
    }
    
    
    function add_letter($data){
         $this->db->insert($this->bbs_letter, $data);
         return $this->db->affected_rows();
    }
    
    function Getreport($id){
        return $this->db->select('*')
          ->from($this->bbs_report)
          ->where('id' , $id)
          ->get()
          ->row_array();
    }
    function opresult($opresult,$id,$user_id){
        $this->db->set('experience','experience+'.$opresult,FALSE);
        $this->db->set('experience_balance','experience_balance+'.$opresult,FALSE);
        $this->db->where('user_id',$user_id);
        $this->db->update($this->bbs_account); 
        return $this->db->where('id',$id)->update($this->bbs_report, array('opresult'=>$opresult,'optime'=>time()));
    }
}
